package com.itheima.reggie.mapper;

import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @Author: Rodgers
 * @Date 2022/3/10 11:19
 */
public interface DishMapper {

    /**
     * 计算菜品的数量
     * @param categoryId
     * @return
     */
    @Select("select count(*) from dish where category_id=#{categoryId} and is_deleted=0")
    Integer dishCount(Long categoryId);

    /**
     * 菜品添加
     * @param dishDto
     * @return
     */
    @Insert("insert into dish values(null,#{name},#{categoryId},#{price},#{code},#{image},#{description},"+
            "#{status},#{sort},#{createTime},#{updateTime},#{createUser},#{updateUser},0)")
    /*
    * 默认情况：数据库表中这条记录有主键，但是dishDto的属性id的为null
    * 目标：执行插入后将数据库生成的id数据返回设置给dishDto对象中id属性
    * 解决方案：@Options,
    * useGeneratedKeys = true 获取生成的主键
    * keyColumn = "id",keyProperty = "id" 将数据库表中生成的id赋值给dishDto对象的属性id
    * */
    @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")
    Integer insert(DishDto dishDto);

    /**
     * 菜品分页查找
     * @return
     */
    List<DishDto> findAll(@Param("name") String name);

    /**
     * 根据菜品id查询一个菜品(一个菜品和多个口味)
     * @param id
     * @return
     */
    DishDto findById(Long id);

    List<Dish> findByCategoryIdAndStatus(@Param("categoryId") Long categoryId,@Param("status") Integer status);
}
